<?php 
namespace Framework;
use App\Controllers\ErrorController;
use Framework\Middleware\Authorise;
class Router{
    protected $routes = [];
    /**
     * 注册一条路由规则
     * 
     * @param string $method
     * @param string $uri
     * @param string $action
     * @param array $middleware
     * @return void
     */
    private function registerRoute($method,$uri,$action,$middleware=[]){
        list($controller,$controllerMethod )= explode('@',$action);
        
        $this->routes[]=[
            'method'=>$method,
            'uri'=>$uri,
            'controller'=>$controller,
            'controllerMethod' => $controllerMethod,
            'middleware'=>$middleware
        ];
    }
     /**
     * 
     * @param string $uri
     * @param string $controller
     * @param array $middleware
     * @return void
     */
    public function addGet($uri,$controller,$middleware=[]){
        $this->registerRoute('GET',$uri,$controller,$middleware);
    }
     /**
     * 
     * @param string $uri
     * @param string $controller
     * @param array $middleware
     * @return void
     */
    public function addPost($uri,$controller,$middleware=[]){
        $this->registerRoute('POST',$uri,$controller,$middleware);
    }
    /**
     * 
     * @param string $uri
     * @param string $controller
     * @param array $middleware
     * @return void
     */
    public function addPut($uri,$controller,$middleware=[]){
        $this->registerRoute('PUT',$uri,$controller,$middleware);
    }
    /**
     * 
     * @param string $uri
     * @param string $controller
     * @param array $middleware
     * @return void
     */
    public function addDelete($uri,$controller,$middleware=[]){
        $this->registerRoute('DELETE',$uri,$controller,$middleware);
    }
    /**
     * 执行路由
     * @param string $uri
     * @param void
     */
    public function route($uri){
        $requestMethod=$_SERVER['REQUEST_METHOD'];
        if($requestMethod === 'POST' $$ isset($_POST['_method'])){
            $requestMethod = strtoupper($_POST['_method']);
        }




        $uriSeqments = explode('/',trim($uri,'/'));
        foreach($this->routes as $route){
            $routeSeqments = explode('/',trim($route['uri'],'/'));
            $match = false;
            if(count($uriSeqments)===count($routeSeqments)&& strtoupper($route['method']===$requestMethod)){
                $params = [];
                $match=true;
                for($i=0;$i<count($uriSeqments);$i++){
                   if($uriSeqments[$i]!==$uriSeqments[$i] && !preg_match('/\{(.+?)\}/',$routeSeqments[$i])){
                        $match=false;
                        break;
                    }
                    if(preg_match('/\{(.+?)\}/',$routeSeqments[$i],$matches)){
                        $params[$matches[1]]=$uriSeqments[$i];
                    }
                }
            }
            if($match){
                foreach($routel['middleware'] as $middleware){(new Authorise())->handle($middleware);
                }


                $controller = 'App\\Controllers\\'.$route['controller'];
                $controllerMethod=$route['controllerMethod'];

                $controllerInstance=new $controller();
                $controllerInstance->$controllerMethod($params);
                return;
            }
               
            
        }
        ErrorController::notFound();
    }
     /**
     * 执行路由
     * @param int $httpCode
     * @return void
     */
    public function error($httpCode = 404){
        http_response_code($httpCode);
        loadView("error/{$httpCode}");
        exit;
    }
    
    
}

